package com.ruoyi.web.controller.leaf;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.LeafSegment;
import com.ruoyi.system.service.LeafSegmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description: TODO
 * @Author: Zhuzhi
 * @Date: 2020/12/13 下午10:10
 */
@Controller
@RequestMapping("/leaf/segment")
public class LeafSegmentController extends BaseController {

    private String prefix = "leaf/segment";
    @Autowired
    LeafSegmentService leafSegmentService;


    @RequiresPermissions("leaf:segment:view")
    @GetMapping()
    public String segment() {
        return prefix + "/segment";
    }

    @RequiresPermissions("leaf:segment:list")
    @PostMapping("/list")
    @ResponseBody
    public List<LeafSegment> list(String namespace) {
        List<LeafSegment> segmentList = leafSegmentService.selectLeafSegmentList(namespace);
        return segmentList;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增
     */
    @RequiresPermissions("leaf:segment:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated LeafSegment leafSegment) {
        if (null != leafSegmentService.selectLeafSegment(leafSegment.getNamespace())) {
            return error("新增序列'" + leafSegment.getNamespace() + "'失败，namespace名称已存在");
        }
        return toAjax(leafSegmentService.insertLeafSegment(leafSegment));
    }

}
